package com.fr.base;

import com.fr.base.core.BaseCoreUtils;
import com.fr.report.core.gif.GIFEncoder;
import com.fr.util.Utils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class BaseUtils
{
  private static Map imageMap = new HashMap();

  public static BufferedImage readImage(String paramString)
  {
    Object localObject = imageMap.get(paramString);
    if (localObject instanceof BufferedImage)
      return ((BufferedImage)localObject);
    InputStream localInputStream = readResource(paramString);
    if (localInputStream == null)
      return null;
    BufferedImage localBufferedImage = null;
    try
    {
      localBufferedImage = readImage(localInputStream);
      localInputStream.close();
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
    }
    imageMap.put(paramString, localBufferedImage);
    return localBufferedImage;
  }

  public static Icon readIcon(String paramString)
  {
    return new ImageIcon(readImage(paramString));
  }

  public static BufferedImage readImage(InputStream paramInputStream)
    throws IOException
  {
    return ImageIO.read(paramInputStream);
  }

  public static BufferedImage readBlobImage(InputStream paramInputStream)
    throws IOException
  {
    return ImageIO.read(new BufferedInputStream(paramInputStream));
  }

  public static void writeImage(Image paramImage, String paramString, OutputStream paramOutputStream)
  {
    try
    {
      if ("gif".equalsIgnoreCase(paramString))
      {
        GIFEncoder localGIFEncoder = new GIFEncoder(paramImage, paramOutputStream);
        localGIFEncoder.encode();
      }
      else
      {
        ImageIO.write(BaseCoreUtils.toBufferedImage(paramImage), paramString, paramOutputStream);
      }
    }
    catch (IOException localIOException)
    {
    }
  }

  public static String convertColumnRowToCellString(ColumnRow paramColumnRow)
  {
    if (paramColumnRow == null)
      return "";
    return paramColumnRow.toString();
  }

  public static ColumnRow convertCellStringToColumnRow(String paramString)
  {
    return ColumnRow.valueOf(paramString);
  }

  public static String readBuildNo()
  {
    return readResourceAsString("build.txt");
  }

  public static String readResourceAsString(String paramString)
  {
    return readResourceAsString(paramString, "GBK");
  }

  public static String readResourceAsString(String paramString1, String paramString2)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    InputStream localInputStream = readResource(paramString1);
    String str = null;
    if (localInputStream != null)
      try
      {
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream, paramString2));
        while ((str = localBufferedReader.readLine()) != null)
        {
          if (localStringBuffer.length() > 0)
            localStringBuffer.append("\n");
          localStringBuffer.append(str);
        }
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
        FRContext.getLogger().log(Level.WARNING, localUnsupportedEncodingException.getMessage(), localUnsupportedEncodingException);
      }
      catch (IOException localIOException)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
      }
    return localStringBuffer.toString();
  }

  public static InputStream readResource(String paramString)
  {
    InputStream localInputStream;
    if ((paramString == null) || (paramString.length() <= 0))
      return null;
    if (paramString.startsWith("/"))
      localInputStream = Utils.class.getResourceAsStream(paramString);
    else
      localInputStream = Utils.class.getResourceAsStream("/" + paramString);
    if (localInputStream != null)
      return localInputStream;
    if (paramString.toLowerCase().startsWith("http:"));
    try
    {
      return new URL(paramString).openStream();
    }
    catch (IOException localIOException2)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException1.getMessage(), localIOException1);
      try
      {
        File localFile = new File(paramString);
        if (localFile.exists())
          return new FileInputStream(paramString);
      }
      catch (SecurityException localSecurityException)
      {
        break label182:
      }
      catch (IOException localIOException2)
      {
        throw new RuntimeException(localIOException2);
      }
    }
    label182: return null;
  }

  public static String generateActiveKey(String paramString)
  {
    byte[] arrayOfByte1 = paramString.getBytes();
    StringBuffer localStringBuffer = new StringBuffer();
    try
    {
      MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
      localMessageDigest.update(arrayOfByte1);
      byte[] arrayOfByte2 = localMessageDigest.digest();
      for (int j = 2; j <= 5; ++j)
      {
        i = arrayOfByte2[j] & 0xFF;
        localStringBuffer.append((char)(i % 25 + 65));
      }
      localStringBuffer.append("-");
      for (j = 7; j <= 11; ++j)
      {
        i = arrayOfByte2[j] & 0xFF;
        if (j == 9)
          localStringBuffer.append(Integer.toHexString(i % 16));
        else
          localStringBuffer.append(i % 10);
      }
      localStringBuffer.append("-");
      int i = arrayOfByte2[15] & 0xFF;
      localStringBuffer.append((char)(i % 25 + 65));
      for (j = 14; j >= 12; --j)
      {
        i = arrayOfByte2[j] & 0xFF;
        localStringBuffer.append(i % 10);
      }
      for (j = 6; j >= 3; --j)
      {
        i = arrayOfByte2[j] & 0xFF;
        localStringBuffer.append(Integer.toHexString(i % 16));
      }
      localStringBuffer.append("-");
      for (j = 4; j >= 1; --j)
      {
        i = arrayOfByte2[j] & 0xFF;
        localStringBuffer.append(i % 10);
      }
      localStringBuffer.insert(5, 'A');
      localStringBuffer.insert(15, 'F');
      System.out.print(localStringBuffer.toString().length());
      return localStringBuffer.toString().toUpperCase();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return "";
  }

  public static String addFileSeparator(String paramString)
  {
    if ((!(paramString.endsWith("/"))) && (!(paramString.endsWith("\\"))))
      return paramString += File.separator;
    return paramString;
  }

  /**
   * @deprecated
   */
  public static String cjkEncode(String paramString)
  {
    return CodeUtils.cjkEncode(paramString);
  }

  /**
   * @deprecated
   */
  public static String cjkDecode(String paramString)
    throws Exception
  {
    return CodeUtils.cjkDecode(paramString);
  }
}